package com.th.apicenter.deduct.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.th.apicenter.deduct.TypeEnum;
import com.th.apicenter.deduct.dto.DeductListDTO;
import com.th.apicenter.deduct.entity.ThDeduct;
import com.th.apicenter.deduct.vo.DeductListVO;
import com.th.apicenter.mapper.ThDeductMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author kay
 * @date 2025/6/9
 * @TODO
 */
@Service
public class ThDeductService {

    @Resource
    private ThDeductMapper thDeductMapper;

    public Map queryTypeAll() {
        List<String> typeAll = thDeductMapper.queryTypeAll();
        Map typeMap = new HashMap();
        for (String type : typeAll) {
            typeMap.put(type, TypeEnum.fromName(type));
        }
        return typeMap;
    }

    public DeductListVO queryListAll(DeductListDTO deductListDTO) {
        DeductListVO deductListVO = new DeductListVO();
        PageHelper.startPage(deductListDTO.getPage(), deductListDTO.getSize());
        List<DeductListVO.DeductList> deductLists = thDeductMapper.queryTypeList(deductListDTO.getType().equals("0") ? null : deductListDTO.getType(), deductListDTO.getAdid());
        PageInfo<DeductListVO.DeductList> pageInfo = new PageInfo<>(deductLists);
        deductListVO.setTotal(pageInfo.getTotal());
        deductListVO.setList(pageInfo.getList());
        return deductListVO;
    }

    public String updateDeduct(String id, String percent) {
        ThDeduct thDeduct = thDeductMapper.selectById(id);
        if (null != thDeduct) {
            thDeduct.setPercent(percent);
            thDeduct.setUpdateTime(new Date());
            thDeductMapper.updateById(thDeduct);
        } else {
            return "未找到该配置！";
        }
        return "操作成功！";
    }
}
